package com.apobates.jforum2.troll.threads.biz.impl;

import com.apobates.jforum2.troll.threads.biz.TopicStatsService;
import com.apobates.jforum2.troll.threads.biz.dao.TopicStatsDao;
import com.apobates.jforum2.troll.threads.entity.TopicStats;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.Optional;
import java.util.stream.Stream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author xiaofanku
 * @since 20200513
 */
@Service
public class TopicStatsServiceImpl implements TopicStatsService{
    @Autowired
    private TopicStatsDao topicStatsDao;
    private final static Logger logger = LoggerFactory.getLogger(TopicStatsServiceImpl.class);

    @Override
    public Stream<TopicStats> getByTopicIdList(Collection<Long> topicIdList) {
        return topicStatsDao.findAllByTopicId(topicIdList);  //DAO会进行检查
    }
    
    @Override
    public Optional<TopicStats> getByTopic(long topicId) {
        return topicStatsDao.findOneByTopic(topicId);
    }

    @Transactional
    @Override
    public Optional<Boolean> updateRecentReply(long topicId, long recentPostsId, long recentPostsMember, String recentPostsMemberNickname) {
        logger.error("[threads-api][SRV] start update topicstats recent posts");
        //统计{话题: 回复数+1}
        //话题的最近回复{topic.rankingDateTime=}
        return topicStatsDao.updatePosts(topicId, recentPostsMember, recentPostsMemberNickname, LocalDateTime.now(), recentPostsId);
    }
}